package com.nand.module.function.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.nand.module.function.R;
import com.nand.module.function.bean.CheckItemsBean;
import com.nand.module.function.bean.PollingBean;
import com.nand.module.function.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/1/3
 */
public class CheckItemsAdapter extends RecyclerView.Adapter<CheckItemsAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private int type;

    private List<CheckItemsBean> mList = new ArrayList<>();

    private CheckItemsAdapter.OnItemClickListener mOnItemClickListener = null;
    private String cont = "";

    public CheckItemsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<CheckItemsBean> data,int type) {
        this.mList = data;
        this.type= type;
        notifyDataSetChanged();
    }

    public List<CheckItemsBean> getCheckItemsBean() {
        return mList;
    }


    @Override
    public CheckItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_check_items, viewGroup, false);
        layout.setOnClickListener(this);
        return new CheckItemsAdapter.ViewHolder(layout);
    }

    private int OrQu = 1;

    @Override
    public void onBindViewHolder(final CheckItemsAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        String iqcCheckStand = mList.get(position).getIqcCheckStand();
        final String iqcCheckContent = mList.get(position).getIqcCheckContent();
        String iqcCheckContentResult = mList.get(position).getIqcCheckContentResult();


        viewHolder.tvJcx.setText(iqcCheckStand);
        viewHolder.tvJcnr.setText(iqcCheckContent);
        viewHolder.radioGraoup.clearCheck();
        switch (type){
            case 0:
                viewHolder.etScz.setHint("请输入");
                for (int i = 0; i < viewHolder.radioGraoup.getChildCount(); i++) {
                    // viewHolder.radioGraoup.getChildAt(i).setBackgroundResource(R.drawable.control_radio);
                    viewHolder.radioGraoup.getChildAt(i).setEnabled(true);
                }
                viewHolder.etScz.setEnabled(true);
                viewHolder.etScz.setText("");
                viewHolder.etScz.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before,
                                              int count) {
                        cont = s.toString();
                        mList.get(position).setIqcCheckContentResult(cont);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });


                viewHolder.radioButton1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mOnItemClickListener!=null){
                            mOnItemClickListener.onRadioClick(mList);
                        }
                    }
                });
                viewHolder.radioButton2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mOnItemClickListener!=null){
                            mOnItemClickListener.onRadioClick(mList);
                        }
                    }
                });
                viewHolder.radioButton1.setChecked(false);
                viewHolder.radioGraoup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (checkedId == R.id.radio_button1) {
                            if (viewHolder.radioButton1.isChecked()) {

                                OrQu = 1;
                                mList.get(position).setResult(OrQu);
                            }
                        } else if (checkedId == R.id.radio_button2) {
                            if (viewHolder.radioButton2.isChecked()) {
                                //     Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString());
                                OrQu = 0;
                                mList.get(position).setResult(OrQu);
                            }
                        }
                    }
                });
                break;
            case 1:
                viewHolder.etScz.setHint("无");
                int result = mList.get(position).getResult();
                viewHolder.etScz.setEnabled(false);
                viewHolder.etScz.setText(iqcCheckContentResult);
                for (int i = 0; i < viewHolder.radioGraoup.getChildCount(); i++) {
                    if (0 == result){
                        LogUtils.e("aaaaaa==="+result);
                        viewHolder.radioButton2.setChecked(true);
                    }else  if (1 == result){
                        viewHolder.radioButton1.setChecked(true);
                        LogUtils.e("aaaaaa==="+result);
                    }
                    viewHolder.radioGraoup.getChildAt(i).setEnabled(false);
                }
                break;
            case 2:
                viewHolder.etScz.setText("");
                viewHolder.etScz.setEnabled(false);
                viewHolder.etScz.setHint("无需输入");
                for (int i = 0; i < viewHolder.radioGraoup.getChildCount(); i++) {
                    // viewHolder.radioGraoup.getChildAt(i).setBackgroundResource(R.drawable.control_radio);
                    viewHolder.radioGraoup.getChildAt(i).setEnabled(false);
                }
                break;
        }


        viewHolder.tvJcnr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onJcnrClick(position,iqcCheckContent,viewHolder.tvJcnr);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        EditText etScz;
        TextView tvJcx, tvJcnr;
        RadioGroup radioGraoup;
        RadioButton radioButton1,radioButton2;

        public ViewHolder(View itemView) {
            super(itemView);
            etScz = itemView.findViewById(R.id.et_scz);
            tvJcx = itemView.findViewById(R.id.tv_jcx);
            tvJcnr = itemView.findViewById(R.id.tv_jcnr);
            radioGraoup = itemView.findViewById(R.id.radio_group);
            radioButton1 = itemView.findViewById(R.id.radio_button1);
            radioButton2 = itemView.findViewById(R.id.radio_button2);

        }

    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onJcnrClick(int position, String context, TextView textView);
        void onRadioClick( List<CheckItemsBean> list);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(CheckItemsAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



